﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id;
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] == null)
                    Response.Redirect("resource.aspx");
                id = Convert.ToInt32(Request.QueryString["id"]);
                Intpage(id);
            }
        }
        private void Intpage(int i)
        {
           // Db_file m_file = new Db_file();
           // DataRow dr = m_file.Read(i);

            study.BLL.tb_file filbll = new study.BLL.tb_file();
            study.Model.tb_file filmod = filbll.GetModel(i);

            if (filmod == null)
            {
                return;
            }
            string fileName = filmod.file_name;//客户端保存的文件名
            string filePath = Server.MapPath("up_file/" + filmod.up_name);//路径

            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
           
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }
}